<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<html>
	<%@include file="/common/simpleFrame.jsp"%>
	<div id="articleEditor">
		<div id="articleTopText1">
			发新帖
		</div>
		<div id="articleTopText2">
			<a href="#" id="newPostBack">返回</a>
		</div>
	
		<!-- 分割线 -->
		<img id="writeArticleHLine1" src="../images/u84.png" />
		<form id="postForm" method="post" action="addNewPost.action">
			<table id="editorAreaTable">
				<tr>
					<td>
						帖子类型：
					</td>
					<td>
						<select id="postType" name="postType" style="width: 150px;height: 22px;">
							<option value="-1">
								请选择类型
							</option>
							<option value="0" selected="selected">
								原创
							</option>
							<option value="1">
								转帖
							</option>
							<option value="2">
								翻译
							</option>
						</select>
					</td>
					<td style="color: #ff6600;font-size: 12px;">
						(原创帖加精的机会更大哦亲！)
					</td>
				</tr>
				<tr>
					<td width="10%">
						标题：
					</td>
					<td width="90%" colspan="2">
						<input id="postTitle" name="postTitle" type="text" style="width:100%;height: 22px;">
					</td>
				</tr>
				<tr>
					<td width="10%">
						版块：
					</td>
					<td width="20%">
						<select id="fBlockId" name="fBlockId" style="width: 150px;height: 22px;">
							<option value="-1">
								选择版块
							</option>
						</select>
					</td>
					<td id="subBlockContainer" width="70%" align="left">
						
					</td>
				</tr>
				<tr>
					<td colspan="3">
						<input type="hidden" id="content" name="content">
						<textarea id="postContent" name="postContent"
								style="width:100%;height:600px;visibility: hidden;"></textarea>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						帖子内容最多2W个字符
					</td>
				</tr>
			</table>
			<div id="attachMentArea">
				
			</div>
			<input type="hidden" id="blockId" name="blockId" value="${blockId}">
		</form>
		<div id="bottomDiv2">
			<button id="submitBtn">
				提交
			</button>
			<button>
				放弃
			</button>
		</div>
	</div>
	
	<link href="${pageContext.request.contextPath}/css/writePost.css" type="text/css" rel="stylesheet" />
	<script charset="utf-8" src="${pageContext.request.contextPath}/common/kindeditor3.5.5/kindeditor.js"></script>
	<script charset="utf-8" src="${pageContext.request.contextPath}/common/kindeditor3.5.5/plugins/code/plugin_code.js"></script>
	<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/common/kindeditor3.5.5/plugins/code/plugin_code.css"/>
	
	<script type="text/javascript">
		$().ready(function(){
			KE.show({
				id : 'postContent',
				afterCreate : function(id) {
					KE.event.ctrl(document, 13, function() {
						KE.sync(id);
					});
					KE.event.ctrl(KE.g[id].iframeDoc, 13, function() {
						KE.sync(id);
					});
				}
			});
			$("#newPostBack").click(function(){
				window.location.href="loadPosts30.action?blockId="+$("#blockId").val();
			})
			$("#submitBtn").click(function(){
				//合法性校验
				var pType=$("#postType").val();
				var fBlock=$("#fBlockId").val();
				var sBlock=$("#subBlockId").val();
				var pTitle=$("#postTitle").val();
				var pContent=KE.text('postContent');
				
				var msg="";
				
				if(pType==-1){
					msg+="必须选择帖子类型哦亲<br>";
				}
				if(fBlock==-1){
					msg+="必须选择版块哦亲<br>";
				}
				if(!pTitle){
					msg+="标题是必填的哦亲<br>";
				}else if(pTitle.length<4||pTitle.length>20){
					msg+="标题长度必须在4-20之间哦亲<br>";
				}
				if(!pContent){
					msg+="没有内容的帖子能说明啥呀亲<br>";
				}else if(pContent.length<20){
					msg+="帖子内容必须大于20个字符哦亲<br>";
				}else if(pContent.length>20000){
					msg+="帖子内容不能大于2W个字符哦亲<br>";
					msg+="当前字符总数:"+pContent.length+"，超出:"+(pContent.length-20000)+"个字<br>";
				}
				var titleReg="";		//校验标题的正则
				var contentReg="";		//校验内容的正则
				
				if(msg){
					showSysMsg(msg,5000);
					return;
				}
				
				$("#blockId").val(sBlock?sBlock:fBlock);//如果有子版，直接传递子版ID，否则直接传递根版块ID
				$("#content").val(KE.html('postContent'));
				$("#postForm").submit();
			})
		})
	</script>
	
	<c:if test="${rootBlocks!=null}">
		<script type="text/javascript">
			var rootBlocks=${rootBlocks};
			var childrenBlocks=${cBlockMap};
			
			//显示子版
			function showSubBlock(pId){
				$("#subBlockContainer").html("");
				if(!pId) return;
				var children=childrenBlocks[pId+''];
				if(!children||children.length==0) return;
				var str='<select id="subBlockId" name="subBlockId" style="width: 150px;height: 22px;">';
				$.each(children,function(index,item){
					str+='<option value="'+item.block_id+'">'+item.block_name+'</option>';
				});
				str+="</select>";
				$("#subBlockContainer").html(str);
			}
			
			$(document).ready(function(){
				//构建论坛版块选项
				$.tpl('rootBlocks',[
					'<option value="{blockId:s}">',
						'{blockName:s}',
					'</option>'
				]);
				
				var resultStr="";
				$.each(rootBlocks,function(idx,item){
					resultStr+=$.tpl('rootBlocks', {
					     blockId: item.block_id,
					     blockName:item.block_name
					}, true);
				});
				$("#fBlockId").append(resultStr);
				$("#fBlockId").change(function(){
					showSubBlock($(this).val());
				});
			})
		</script>
	</c:if>
</html>